<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Sning 添加文章</title>
<meta name="keywords" content="个人博客,Sning个人博客,宋高明" />
<meta name="description" content="Sning个人博客，学无止境，勇于探索。" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="static/css/base.css" rel="stylesheet">
<link href="static/css/index.css" rel="stylesheet">
<link href="static/css/m.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="js/modernizr.js"></script>
<![endif]-->
</head>
<body>
<header>
  <div id="mnav">
    <div class="logo"><a href="/">Sning个人博客</a></div>
    <h2 id="mnavh"><span class="navicon"></span></h2>
    <ul id="starlist">
        <li><a href="index.html">首页</a></li>
        <li><a href="blog_list=9900.html">博客</a></li>
        <li><a href="write.html">写作</a></li>
        <li><a href="share.html">项目</a></li>
        <li><a href="file.html">文件</a></li>
        <li><a href="about.html">关于</a></li>
    </ul>
  </div>
<script>
window.onload = function ()
{
  var oH2 = document.getElementById("mnavh");
  var oUl = document.getElementById("starlist");
  oH2.onclick = function ()
  {
    var style = oUl.style;
    style.display = style.display == "block" ? "none" : "block";
    oH2.className = style.display == "block" ? "open" : ""
  }
var obj=null;
var As=document.getElementById('starlist').getElementsByTagName('a');
obj = As[0];
for(i=1;i<As.length;i++){if(window.location.href.indexOf(As[i].href)>=0)
obj=As[i];}
obj.id='selected'
}
</script>
</header>
<article>
  <h1 class="t_nav"><span>合抱之木，生于毫末；九层之台，起于垒土；千里之行，始于足下。 </span>
    <a href="/" class="n1">首 页</a><a href="write.html" class="n2">写 作</a></h1>
      <div class="document-editor">
        <div class="document-editor__toolbar"></div>
        <div class="document-editor__editable-container">
            <div class="document-editor__editable" id='editor'>
            </div>
        </div>
      </div>
   <div class="w_sidebar">
      <div class="art_info">
        <form id="article_all">
          用 户 id: <input id="u_id" size="20" type="text" name="u_id" value="0" /><br /><br />
          标 题 &nbsp;&nbsp; : <input id="title" size="20" type="text" name="title" placeholder="标题" /><br /><br />
          简 述 &nbsp;&nbsp; : <br /><textarea cols="30" rows="5" type="text" name="summary" placeholder="文章描述"></textarea><br /><br />
          关 键 词: <input size="20" type="text" name="key_words" placeholder="以|分割" /><br /><br />
             
              <div class="txt-box">博客分类：
                  <select id="selectbox" name="cate">
                      <option value="0">选择分类</option>
                      <option value='1'>Python</option>
                      <option value='2'>C/C++</option>
                      <option value='3'>Java</option>
                      <option value='4'>Android</option>
                      <option value='5'>Html/Css/js</option>
                      <option value='6'>Linux</option>
                      <option value='7'>Mysql/MariaDB</option>
                      <option value='8'>文章收集</option>
                      <option value='10'>Other</option>
                  </select>
              </div><br />
        </form> 
        <button  class="btn_fun" onclick="submitart()"> 发 布 </button>
        <button id="btn1" class="btn_fun"> 清 空 </button>
        <button id="btn2" class="btn_fun"> 显 示 h5 </button>
       </div>
    </div>
</article>
<div class="blank" style="min-height=10px;height=100%"></div>
<footer>
  <p>Design by <a href="/">Sning</a>
  <a href="http://www.yangqq.com" target="_blank">模板来自：杨青个人博客</a>
  </p>
</footer>
<script src="static/ckeditor/ckeditor.js"></script>
<link href="static/ckeditor/editor.css" rel="stylesheet">
<script>
SningEditor
    .create( document.querySelector( '#editor' ),{
        ckfinder: {
            uploadUrl : 'uploadImages'
            //后端处理上传逻辑返回json数据,包括uploaded(选项true/false)和url两个字段
        }
    })
    .then( editor => {
        const toolbarContainer = document.querySelector( '.document-editor__toolbar' );
        window.editor = editor;
        toolbarContainer.appendChild( editor.ui.view.toolbar.element );
    } )
    .catch( error => {
        console.error( error );
    } );
    document.getElementById('btn1').addEventListener('click', function () {
        // 读取 html
        editor.setData("")
    }, false)

    document.getElementById('btn2').addEventListener('click', function () {
        // 读取 text
        const data = editor.getData();
        alert(data)
        // editor.setData( '<p>Some bt2</p>' );
    }, false)
    function submitart() {  
        var uid=document.getElementById('u_id')
        var title=document.getElementById('title')
        if(uid.value==''||title.value==''){
          alert("用户id 或 标题为空")
          return
        }
        var select=document.getElementById('selectbox')
        var s=select.value
        if(s=="0"){
          alert("请选择文章分类")
        }
        var form =document.getElementById("article_all")
        var formdata = new FormData(form);
        const data = editor.getData();  
        formdata.append("content", data);
        formdata.append("category",s);
        var xhr=new XMLHttpRequest();
          xhr.open("post","/createart");
          xhr.send(formdata);
          xhr.onload=function(){
              if(xhr.status==200){
                // editor.setData("")
                alert("文章添加成功")
              }
              if(xhr.status==201){
                //editor.setData("")
                alert("error")
              }
          }
    }  
</script>
</body>
</html>